package com.yfbao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfbao.encryptor.AESEncryptor;
import com.yfbao.encryptor.EncryptLambdaQueryWrapper;
import com.yfbao.model.UserDto;
import com.yfbao.service.UserServiceImpl;
import com.yfbao.service.mapper.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/")
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private AESEncryptor encryptor;

    @GetMapping("/add")
    public Object add() {
        for (long i = 0; i < 5; i++) {
            UserDto user = new UserDto();
            user.setUsername("test");
            user.setPhone("12345678901");
            userService.save(user);
        }
        return "success";
    }

    @GetMapping("/add_new")
    public Object add2() {
        for (long i = 0; i < 5; i++) {
            UserDto user = new UserDto();
            user.setUsername("test");
            user.setPhone("12345678901");
            user.setEmail("qqqq@qq.com");
            userService.save(user);
        }
        return "success";
    }

    @GetMapping("/update")
    public Object update() {
        UserDto byId = userService.getById(100L);
        log.info(byId.toString());
        return "success";
    }

    @GetMapping("/list")
    public Object get() {
        return userService.list();
    }

    @GetMapping("/users/phone")
    public Object get(@RequestParam("phone") String phone) {
        EncryptLambdaQueryWrapper<UserDto> wrapper = new EncryptLambdaQueryWrapper<UserDto>()
                .setEncryptor(encryptor)
                .eq(UserDto::getPhone, phone);
        Page<UserDto> page =new Page<>(1,20);
        return userService.page(page,wrapper);
    }


}
